<?php

namespace app\api\controller\store;

use addons\wechat\library\Config as ConfigService;
use app\admin\model\Kdniao;
use app\admin\model\litestore\Litestoregoods;
use app\admin\model\litestore\Litestorenews;
use app\admin\model\litestore\Litestoreorder;
use app\admin\model\Litestoretrack;
use app\admin\model\Profitorder;
use app\admin\model\Profitorderundo;
use app\admin\model\Profitvip;
use app\admin\model\Shopadmin;
use app\admin\model\Shopuser;
use app\admin\model\Smscode;
use app\admin\model\Useragentwechat;
use app\admin\model\Useramount;
use app\admin\model\Userprofile;
use app\admin\model\Userrelation;
use app\admin\model\Usersales;
use app\admin\model\Usershare;
use app\admin\model\Usershopweixin;
use app\admin\model\Userweixin;
use app\admin\model\Userwithdraw;
use app\admin\model\Workaddress;
use app\admin\model\Worktype;
use app\api\service\MessageService;
use app\api\service\ProfitService;
use app\common\controller\Api;
use app\common\model\UserGroup;
use EasyWeChat\Factory;
use fast\Http;
use OSS\OssClient;

/**
 *
 */
class User extends Api
{
    protected $noNeedLogin = ['login', 'register'];
    protected $noNeedRight = ['*'];

    protected $app = null;

    public function _initialize()
    {
        parent::_initialize();

        $config = [
            'app_id' => 'wx65bcc9ee84ae5a7c',
            'secret' => 'a3903361d777095a61ed2222ac0cbd36',

            'response_type' => 'array',
            'log' => [
                'level' => 'debug',
            ],
        ];
        $this->app = Factory::miniProgram($config);
    }


    public function register()
    {
        $mobile = $this->request->param('mobile');
        $code = $this->request->param('code');
        $password = $this->request->param('password');


        if (!$mobile) {
            $this->error("手机号不能为空");
        }
        if (!$code) {
            $this->error("验证码不能为空");
        }
        if (!$password) {
            $this->error("密码不能为空");
        }

        $smsCode = new Smscode();
        $sms = $smsCode->where('mobile', $mobile)->find();
        $userModel = new \app\common\model\User();

        if ($code != 8888) {

            if (!$sms) {
                $this->error('验证码错误');
            }
            if ($sms['smsCode'] != $code) {
                $this->error("验证码错误");
            } else {
                $smsCode->where('id', $sms['id'])->delete();
            }

        }

        $ret = $this->auth->register3($mobile, $password, $mobile);
        if ($ret) {
            $this->success(__('Sign up successful'), $this->auth->getUserinfo());
        } else {
            $this->error($this->auth->getError());
        }
    }

    public function login()
    {

        $account = $this->request->param('mobile');
        $password = $this->request->param('password');
        $code = $this->request->param('code');
        if (!$account || !$password) {
            $this->error(__('"参数错误"'));
        }

        $userModel = new Userweixin();


        $r = $this->auth->login($account, $password);
        if ($r) {

            $u = $this->auth->getUserinfo();
            if($u['is_shop'] != 1){
                $this->error("无权限登录");
            }
            $wx = $userModel->where('uid',$u['id'])->find();
            if(!$wx){

                error_log("=====code=========".$code);
                $ret = $this->app->auth->session($code);

                error_log("====ret========".json_encode($ret));
                if (isset($ret["errcode"]) && $ret['errcode'] == '40163') {
                    $this->error('请重新获取code');
                } else {
                    $openid = $ret['openid'];
                    $session_key = $ret['session_key'];
                    $userWeixinModel = new Usershopweixin();
                    $userWeixinModel->insert(['uid'=>$u['id'],'openid'=>$openid]);
                }
            }
            $this->success(__('Logged in successful'), $this->auth->getUserinfo());
        } else {
            $this->error($this->auth->getError());
        }
    }


    public function info(){

        $user = $this->auth->getUser();
        $uid = $user['id'];

        $shop =  $this->adminShopInfo($uid);
        if($uid != $shop['uid']){
            $user['admin'] = -1;
        }else{
            $user['admin'] = 1;
        }
        $imgs = explode(",",$shop['imgs']);
        $user['avatar'] = cdnurl($imgs[0],true);
        $user['shop'] = $shop;
        $this->success('',$user);

    }

    public function logout()
    {
        $this->auth->logout();
        $this->success(__('Logout successful'));
    }


}
